{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% import "_macros.html" as macros %}

{% block title %}语音识别模型基准测试系统{% endblock %}

{% block page_content %}
<div class="page-header">
    <a href="{{ url_for('.dataset') }}">返回数据集总页</a>
    <p id="dataset-id" style="display:none">{{ dataset.id }}</p>
    <p>名称: {{ dataset.name }}</p>
    <p>备注: {{ dataset.description }}</p>
    <p>路径: {{ dataset.path }}</p>
    <p>语音文件扩展名：{{ dataset.wav_ext }}</p>
    <p>语音的转录文本的存储方式: {% if dataset.wav2trn_type_is_summary %}已汇总{% else %}一一对应{% endif %}</p>
    <a class="btn btn-default" href="javascript:void(0)" onclick="start_process_added_dataset({{ dataset.id }})">开始处理数据子集</a>
    <a class="btn btn-default" href="{{ url_for('.dataset_edit',id=dataset.id) }}">修改数据集信息</a>
    <a class="btn btn-default" href="{{ url_for('.dataset_json',id=dataset.id) }}">导出数据集的json配置文件</a><br>
    <a class="btn btn-default" href="{{ url_for('.dataset_active',id=dataset.id) }}">更新活动时间</a>
    <a class="btn btn-default" href="{{ url_for('.dataset_copy',id=dataset.id) }}">以此为模版新建</a><br>
    <a class="btn btn-default" href="javascript:void(0)" onclick="start_validate_dataset({{ dataset.id }})">校验语音文件的格式</a>
    <h1>数据子集</h1>
    <div class="row">
        <table class="table">
            <thead>
                <tr>
                    <td>名称</td>
                    <td>相对数据集目录的数据子集目录</td>
                    <td>转录文本文件扩展名</td>
                    <td>相对数据集目录的转录文本文件所在的目录</td>
                    <td>语音的转录文本的文件名</td>
                    <td>转录文本在文件中的行号（从0计）</td>
                    <td>转录文本文件格式</td>
                    <td>语音文件数量</td>
                    <td>语音时长的统计学特征，依次是平均值、中值、高频值、总值和最大值</td>
                    <td>语音的采样率</td>
                    <td>语音的通道数</td>
                    <td>语音的采样带宽</td>
                    <td>操作</td>
                </tr>
            </thead>
            <tbody id="datasubsets">
                <tr>
                    <td>训练集</td>
                    <td>{{ train.subdir }}</td>
                    <td>{{ train.trn_ext }}</td>
                    <td>{{ train.trn_dir }}</td>
                    <td>{{ train.trn_file }}</td>
                    <td>{{ train.line_no }}</td>
                    <td>{% if train.trn_file_format==1 %}txt{% elif train.trn_file_format==2 %}json{% endif %}</td>
                    <td>{{ train.num }}</td>
                    <td>{{ train.audio_duration_mean }}, {{ train.audio_duration_median }}, {{ train.audio_duration_mode }}, {{ train.audio_duration_sum }}, {{ train.audio_duration_max }}</td>
                    <td>{{ train.sample_rate }}</td>
                    <td>{{ train.channel_num }}</td>
                    <td>{{ train.sample_width }}</td>
                    <td><a class="btn btn-default" href="{{ url_for('.datasubset_edit',id=train.id) }}">修改</a></td>
                </tr>
                <tr>
                    <td>验证集</td>
                    <td>{{ val.subdir }}</td>
                    <td>{{ val.trn_ext }}</td>
                    <td>{{ val.trn_dir }}</td>
                    <td>{{ val.trn_file }}</td>
                    <td>{{ val.line_no }}</td>
                    <td>{% if val.trn_file_format==1 %}txt{% elif val.trn_file_format==2 %}json{% endif %}</td>
                    <td>{{ val.num }}</td>
                    <td>{{ val.audio_duration_mean }} {{ val.audio_duration_median }} {{ val.audio_duration_mode }} {{ val.audio_duration_sum }} {{ val.audio_duration_max }}</td>
                    <td>{{ val.sample_rate }}</td>
                    <td>{{ val.channel_num }}</td>
                    <td>{{ val.sample_width }}</td>
                    <td><a class="btn btn-default" href="{{ url_for('.datasubset_edit',id=val.id) }}">修改</a></td>
                </tr>
                <tr>
                    <td>测试集</td>
                    <td>{{ test.subdir }}</td>
                    <td>{{ test.trn_ext }}</td>
                    <td>{{ test.trn_dir }}</td>
                    <td>{{ test.trn_file }}</td>
                    <td>{{ test.line_no }}</td>
                    <td>{% if test.trn_file_format==1 %}txt{% elif test.trn_file_format==2 %}json{% endif %}</td>
                    <td>{{ test.num }}</td>
                    <td>{{ test.audio_duration_mean }} {{ test.audio_duration_median }} {{ test.audio_duration_mode }} {{ test.audio_duration_sum }} {{ test.audio_duration_max }}</td>
                    <td>{{ test.sample_rate }}</td>
                    <td>{{ test.channel_num }}</td>
                    <td>{{ test.sample_width }}</td>
                    <td><a class="btn btn-default" href="{{ url_for('.datasubset_edit',id=test.id) }}">修改</a></td>
                </tr>
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="13">
                        <span id="span-1"></span><br>
                        <span id="span-2"></span><br>
                        <span id="span-3"></span>
                    </td>
                </tr>
            </tfoot>
        </table>
    </div>
    <div class="row" id="validateDatasetDiv">

    </div>
    <button id="normalizeBtn" style="display: none;">格式规范化</button>
    {% if bttask_names %}
    <p>
        数据集的基准测试用例有<br>
        {% for i in range(bttask_names|length) %}
        <a href="{{ url_for('.btt',id=bttask_ids[i]) }}">{{ bttask_names[i] }}</a><br>
        {% endfor %}
    </p>
    {% endif %}
</div>
<script type="text/javascript">
    const normalizeBtn=document.getElementById('normalizeBtn');
    function start_process_added_dataset(dataset_id){
        $.ajax({
            type:'POST',
            url:'/process_added_dataset/'+dataset_id,
            success:function(data,status,request){
                status_url=request.getResponseHeader('Location');
                update_status(status_url);
            },
            error:function(){
                alert('请求预处理数据集的信息的过程中出现错误');
            }
        });
    }
    function update_status(status_url){
        $.getJSON(status_url,function(data){
            $('#span-1').text($('#span-2').text());
            $('#span-2').text($('#span-3').text());
            if ('result' in data){
                $('#span-3').text(data['time']+' '+data['status']);
                let result=$.parseJSON(data['result']);
                console.log(result);
                let train=result['train'];
                let val=result['val'];
                let test=result['test'];
                let tbody_html='';
                $('#datasubsets').empty();
                tbody_html+='<tr><td>训练集</td><td>'+train['subdir']+'</td><td>'+train['trn_ext']+'</td><td>'+train['trn_dir']+'</td><td>'+train['trn_file']+'</td><td>'+train['line_no']+'</td><td>';
                if(train['trn_file_format']==1){
                    tbody_html+='txt';
                }
                else if(train['trn_file_format']==2){
                    tbody_html+='json';
                }
                tbody_html+='</td><td>'+train['num']+'</td><td>'+train['audio_duration_statistics'][0]+' '+train['audio_duration_statistics'][1]+' '+train['audio_duration_statistics'][2]+' '+train['audio_duration_statistics'][3]+' '+train['audio_duration_statistics'][4]+'</td><td>'+train['sample_rate']+'</td><td>'+train['channel_num']+'</td><td>'+train['sample_width']+'</td><td><a class="btn btn-default" href="/datasubset_edit/'+train['id']+'">修改</a></td></tr>';
                tbody_html+='<tr><td>验证集</td><td>'+val['subdir']+'</td><td>'+val['trn_ext']+'</td><td>'+val['trn_dir']+'</td><td>'+val['trn_file']+'</td><td>'+val['line_no']+'</td><td>';
                if(val['trn_file_format']==1){
                    tbody_html+='txt';
                }
                else if(val['trn_file_format']==2){
                    tbody_html+='json';
                }
                tbody_html+='</td><td>'+val['num']+'</td><td>'+val['audio_duration_statistics'][0]+' '+val['audio_duration_statistics'][1]+' '+val['audio_duration_statistics'][2]+' '+val['audio_duration_statistics'][3]+' '+val['audio_duration_statistics'][4]+'</td><td>'+val['sample_rate']+'</td><td>'+val['channel_num']+'</td><td>'+val['sample_width']+'</td><td><a class="btn btn-default" href="/datasubset_edit/'+val['id']+'">修改</a></td></tr>';
                tbody_html+='<tr><td>测试集</td><td>'+test['subdir']+'</td><td>'+test['trn_ext']+'</td><td>'+test['trn_dir']+'</td><td>'+test['trn_file']+'</td><td>'+test['line_no']+'</td><td>';
                if(test['trn_file_format']==1){
                    tbody_html+='txt';
                }
                else if(test['trn_file_format']==2){
                    tbody_html+='json';
                }
                tbody_html+='</td><td>'+test['num']+'</td><td>'+test['audio_duration_statistics'][0]+' '+test['audio_duration_statistics'][1]+' '+test['audio_duration_statistics'][2]+' '+test['audio_duration_statistics'][3]+' '+test['audio_duration_statistics'][4]+'</td><td>'+test['sample_rate']+'</td><td>'+test['channel_num']+'</td><td>'+test['sample_width']+'</td><td><a class="btn btn-default" href="/datasubset_edit/'+test['id']+'">修改</a></td></tr>';
                $('#datasubsets').append(tbody_html);
                // console.log(data['result']);
                // window.location.reload();
            }else{
                $('#span-3').text(data['time']+' '+data['state']+' '+data['status']);
            }
            if(data['state']=='PENDING'||data['state']=='PROGRESS'){
                setTimeout(function(){
                    update_status(status_url);
                },1000);
            }
        });
    }

    function start_validate_dataset(dataset_id){
        $.ajax({
            type:'POST',
            url:'/dataset_validate/'+dataset_id,
            success:function(data,status,request){
                status_url=request.getResponseHeader('Location');
                update_validate_dataset_status(status_url,dataset_id);
            },
            error:function(){
                alert('请求校验数据集的语音文件格式的过程中出现错误');
            }
        });
    }

    function update_normalize_dataset_status(status_url){
        $.getJSON(status_url,function(data){
            $('#span-1').text($('#span-2').text());
            $('#span-2').text($('#span-3').text());
            if('result' in data){
                console.log(data);
                $('#span-3').text(data['time']+' '+data['status']+' '+data['result']);
            }else{
                $('#span-3').text(data['time']+' '+data['state']+data['status']);
            }
            if(data['state']=='PENDING'||data['state']=='PROGRESS'){
                setTimeout(function(){
                    update_normalize_dataset_status(status_url);
                },1000);
            }
        });
    }

    normalizeBtn.onclick=()=>{
        console.log('开始规范化');
        const fdata=new FormData(document.getElementById('normalizeDatasetForm'));
        $.ajax({
            type:'POST',
            url:'/normalize_dataset',
            data:fdata,
            processData:false,
            contentType:false,
            error:function(data){
                alert('请求规范化数据集的语音文件格式的过程中出现错误');
            },
            success:function(data,status,request){
                status_url=request.getResponseHeader('Location');
                update_status(status_url);
            }
        });
        $('#validateDatasetDiv').empty();
        normalizeBtn.setAttribute('style','display:none;');
    }

    function update_validate_dataset_status(status_url,dataset_id){
        $.getJSON(status_url,function(data){
            $('#span-1').text($('#span-2').text());
            $('#span-2').text($('#span-3').text());
            if('result' in data){
                $('#span-3').text(data['time']+' '+data['status']);
                let result=$.parseJSON(data['result']);
                console.log(result);
                let div_html='';
                let bNormalize=false;
                div_html+='<form id="normalizeDatasetForm" enctype="application/x-www-form-urlencoded">\n<input name="dataset_id" value="'+dataset_id+'" type="hidden">\n'
                div_html+='<label>声道数</label>'
                if(result['nchannel'].length==1){
                    div_html+='<select name="target_nchannel"><option value="'+result['nchannel'][0][0]+'" selected="selected">'+result['nchannel'][0][0]+' '+result['nchannel'][0][1]+'条语音文件</option></select><br>'
                }else{
                    bNormalize=true
                    div_html+='<select name="target_nchannel"><option value="'+result['nchannel'][0][0]+'" selected="selected">'+result['nchannel'][0][0]+' '+result['nchannel'][0][1]+'条语音文件</option>'
                    for(let i=1;i<result['nchannel'].length;i++){
                        div_html+='<option value="'+result['nchannel'][i][0]+'">'+result['nchannel'][i][0]+' '+result['nchannel'][i][1]+'条语音文件</option>'
                    }
                    div_html+='</select><br>'
                }
                div_html+='<label>采样率</label>'
                if(result['sample_rate'].length==1){
                    div_html+='<select name="target_sr"><option value="'+result['sample_rate'][0][0]+'" selected="selected">'+result['sample_rate'][0][0]+' '+result['sample_rate'][0][1]+'条语音文件</option></select><br>'
                }else{
                    bNormalize=true
                    div_html+='<select name="target_sr"><option value="'+result['sample_rate'][0][0]+'" selected="selected">'+result['sample_rate'][0][0]+' '+result['sample_rate'][0][1]+'条语音文件</option>'
                    for(let i=1;i<result['sample_rate'].length;i++){
                        div_html+='<option value="'+result['sample_rate'][i][0]+'">'+result['sample_rate'][i][0]+' '+result['sample_rate'][i][1]+'条语音文件</option>'
                    }
                    div_html+='</select><br>'
                }
                div_html+='<label>位深度，目前只能转化为16位的语音文件</label>'
                if(result['sample_width'].length==1){
                    div_html+='<select name="target_sw"><option value="'+result['sample_width'][0][0]+'" selected="selected">'+result['sample_width'][0][0]+' '+result['sample_width'][0][1]+'条语音文件</option></select><br>'
                }else{
                    bNormalize=true
                    div_html+='<select name="target_sw"><option value="'+result['sample_width'][0][0]+'" selected="selected">'+result['sample_width'][0][0]+' '+result['sample_width'][0][1]+'条语音文件</option>'
                    for(let i=1;i<result['sample_width'].length;i++){
                        div_html+='<option value="'+result['sample_width'][i][0]+'">'+result['sample_width'][i][0]+' '+result['sample_width'][i][1]+'条语音文件</option>'
                    }
                    div_html+='</select><br>'
                }
                if(bNormalize){
                    normalizeBtn.setAttribute('style','display:block;');
                }
                div_html+='</form>\n'
                $('#validateDatasetDiv').append(div_html);
                let train=result['train'];
                let val=result['val'];
                let test=result['test'];
                let tbody_html='';
                $('#datasubsets').empty();
                tbody_html+='<tr><td>训练集</td><td>'+train['subdir']+'</td><td>'+train['trn_ext']+'</td><td>'+train['trn_dir']+'</td><td>'+train['trn_file']+'</td><td>'+train['line_no']+'</td><td>';
                if(train['trn_file_format']==1){
                    tbody_html+='txt';
                }
                else if(train['trn_file_format']==2){
                    tbody_html+='json';
                }
                tbody_html+='</td><td>'+train['num']+'</td><td>'+train['audio_duration_statistics'][0]+' '+train['audio_duration_statistics'][1]+' '+train['audio_duration_statistics'][2]+' '+train['audio_duration_statistics'][3]+' '+train['audio_duration_statistics'][4]+'</td><td>'+train['sample_rate']+'</td><td>'+train['channel_num']+'</td><td>'+train['sample_width']+'</td><td><a class="btn btn-default" href="/datasubset_edit/'+train['id']+'">修改</a></td></tr>';
                tbody_html+='<tr><td>验证集</td><td>'+val['subdir']+'</td><td>'+val['trn_ext']+'</td><td>'+val['trn_dir']+'</td><td>'+val['trn_file']+'</td><td>'+val['line_no']+'</td><td>';
                if(val['trn_file_format']==1){
                    tbody_html+='txt';
                }
                else if(val['trn_file_format']==2){
                    tbody_html+='json';
                }
                tbody_html+='</td><td>'+val['num']+'</td><td>'+val['audio_duration_statistics'][0]+' '+val['audio_duration_statistics'][1]+' '+val['audio_duration_statistics'][2]+' '+val['audio_duration_statistics'][3]+' '+val['audio_duration_statistics'][4]+'</td><td>'+val['sample_rate']+'</td><td>'+val['channel_num']+'</td><td>'+val['sample_width']+'</td><td><a class="btn btn-default" href="/datasubset_edit/'+val['id']+'">修改</a></td></tr>';
                tbody_html+='<tr><td>测试集</td><td>'+test['subdir']+'</td><td>'+test['trn_ext']+'</td><td>'+test['trn_dir']+'</td><td>'+test['trn_file']+'</td><td>'+test['line_no']+'</td><td>';
                if(test['trn_file_format']==1){
                    tbody_html+='txt';
                }
                else if(test['trn_file_format']==2){
                    tbody_html+='json';
                }
                tbody_html+='</td><td>'+test['num']+'</td><td>'+test['audio_duration_statistics'][0]+' '+test['audio_duration_statistics'][1]+' '+test['audio_duration_statistics'][2]+' '+test['audio_duration_statistics'][3]+' '+test['audio_duration_statistics'][4]+'</td><td>'+test['sample_rate']+'</td><td>'+test['channel_num']+'</td><td>'+test['sample_width']+'</td><td><a class="btn btn-default" href="/datasubset_edit/'+test['id']+'">修改</a></td></tr>';
                $('#datasubsets').append(tbody_html);
            }else{
                $('#span-3').text(data['time']+' '+data['state']+' '+data['status']);
            }
            if(data['state']=='PENDING'||data['state']=='PROGRESS'){
                setTimeout(function(){
                    update_validate_dataset_status(status_url,dataset_id);
                },1000);
            }
        });
    }
</script>
{% if not is_intialized %}
<script type="text/javascript">
    window.onload=function(){
        start_process_added_dataset(parseInt($('#dataset-id').text()));
    };
</script>
{% endif %}
{% endblock %}